package com.example.centertest;

import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText et1 =(EditText) findViewById(R.id.editTextTextPersonName);
        Button bt1 =(Button) findViewById(R.id.button);
        et1.setOnKeyListener((v, keyCode, event) -> {
            String text = et1.getText().toString();
            if(keyCode<65&&keyCode>122){
                et1.setText(text.substring(0,text.length()-1));
            }
            return true;
        });

        bt1.setOnClickListener(v -> {
            Intent intent = new Intent(this,SecondActivity.class);
            Bundle bd=new Bundle();
            bd.putString("zh",et1.getText().toString());
            intent.putExtras(bd);
            startActivityForResult(intent,-1);
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0,0,0,"关于");
        menu.add(0,1,1,"退出");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        AlertDialog.Builder b=new AlertDialog.Builder(this);
        if(item.getItemId()==0){
            b.setTitle("关于");
            b.setMessage("朱传杰");
            b.setPositiveButton("确定",null);
        }else {
            b.setTitle("退出");
            b.setMessage("你确定要退出吗");
            b.setPositiveButton("确定", (dialog, which) -> {
                   finish();
            });
            b.setNegativeButton("取消",null);
        }
        b.show();
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Toast.makeText(this, "欢迎您参加期中考试!!!",Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("debug","您已返回FirstActivity界面");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "你已退出其中考试!!!",Toast.LENGTH_LONG).show();
    }
}